package chapter07_object;

/**
 * ClassName: ToStringTest
 * Package: chapter07_object
 * Description:
 *
 * Object类中的toSting()方法
 *
 * @Author SherryAn
 * @Create 2025/7/19 18:48
 */
public class ToStringTest {
    public static void main(String[] args) {
        User2 u1=new User2("Tom",12);
        System.out.println(u1.toString());//chapter07_object.User2@3b07d329
        //重写toString()后输出：User{name=Tom，age=12}
        System.out.println(u1);//chapter07_object.User2@3b07d329
        //重写toString()后输出：User{name=Tom，age=12}
        String s1=new String("Hello");
        System.out.println(s1.toString());//Hello
    }
}
class User2{
    String name;
    int age;

    //构造器
    public User2(){

    }

    public User2(String name, int age) {
        this.name = name;
        this.age = age;
    }

//    @Override
//    public String toString() {
//        return "User{name="+name+"，age="+age+"}";
//    }


    @Override
    public String toString() {
        return "User2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

